import pandas as pd
from sklearn import preprocessing
import numpy as np


# path = 'F:\代码\代码\python\\test.xlsx'

# TODO
def _func():
    pass

# 定义相关函数计算
def CCF(x: np.ndarray, y: np.ndarray) -> list:  # TODO: type hint
    """
    TODO: 函数目的
    :param x: 一组一维序列
    :param y: 和x长度要一致
    """
    # TODO:
    try:
        assert len(x) == len(y)
    except:
        raise ValueError("输入长度不一致")

    _func()

    # mean centering and scaling to unit standard deviation
    x_ = preprocessing.scale(x, axis=0, with_mean=True, with_std=True, copy=True)
    y_ = preprocessing.scale(y, axis=0, with_mean=True, with_std=True, copy=True)
    ave = []
    # 求不同滞后时间下的CCF函数值，并存入ave列表中
    for lamda in range(0, N // 4):
        z = x_[0:N - lamda] * y_[lamda:N]
        ave.append(z.sum() / (N - lamda))
    print('max：', max(ave))
    print('min：', min(ave))
    return [ave.index(max(ave)), max(ave), ave.index(min(ave)), min(ave)]


    #  # 比较后返回时间滞后值
    # if max(ave)+min(ave)>0:
    #     return [ave.index(max(ave)),max(ave)]
    # else:
    #     return [ave.index(min(ave)),min(ave)]


def check_value(z):
    rou = max(z[1], abs(z[3]))
    pusai = 2 * abs(z[1] + z[3]) / (z[1] + abs(z[3]))
    if rou >= 1.85 * N ** -0.41 + 2.37 * N ** -0.53 and pusai >= 0.46 * N ** -0.16:
        return z
    else:
        print('检查不通过')


if __name__ == '__main__':
    # 采样数
    N = 10000
    samples_n = 11000
    tau_0 = 100
    STD = 0.01
    # 采样时间
    T = 1
    # 导入数据
    # df = pd.read_excel(path)
    # x = df[df.columns[1]].to_numpy()
    # y = df[df.columns[]].to_numpy()
    np.random.seed(1)
    # x_0 = 2.0 * np.random.random(samples_n) - 1.0
    x_0 = np.random.normal(0.0, 9.0, size=samples_n)
    y_0 = np.hstack((
        np.zeros(tau_0),
        # np.random.normal(中心，标准差，数量)
        0.2 * x_0[: -tau_0] + np.hstack((np.zeros(tau_0 + 1), x_0[:-(tau_0 + 1)])) + np.random.normal(0, STD,
                                                                                                      len(x_0) - tau_0)
    ))
    x = x_0[tau_0:N + tau_0]
    y = y_0[tau_0:N + tau_0]

    z = CCF(x, y)
    """    
    z = check_value(z)
    """
    print(z)
